package top.korori.controller;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import top.korori.common.model.Result;
import top.korori.system.entity.bo.LoginBo;
import top.korori.system.service.SysAuthService;
import top.korori.system.service.SysMenuService;
import top.korori.system.entity.vo.SysLoginUserVo;
import top.korori.system.entity.vo.SysMenuVo;

import java.util.List;

@Api(tags = "身份认证")
@RestController
@RequestMapping("/sys/auth")
public class SysAuthController {

    @Autowired
    private SysAuthService sysAuthService;

    @Autowired
    private SysMenuService sysMenuService;

    @ApiOperation("管理系统登录")
    @PostMapping("/login")
    public Result<?> login(@RequestBody LoginBo loginBo){
        String token = sysAuthService.login(loginBo);
        return Result.okAndPut("token",token);
    }

    @ApiOperation("获取登录用户信息")
    @GetMapping("/getUserInfo")
    public Result<SysLoginUserVo> getLoginUser(){
       SysLoginUserVo sysLoginUserVo = sysAuthService.getLoginUser();
       return Result.ok(sysLoginUserVo);
    }

    @ApiOperation("用户菜单")
    @GetMapping("/treeMenus")
    public Result<List<SysMenuVo>> treeMenus(){
        Long userId = sysAuthService.getUserId();
        List<SysMenuVo> sysMenuVos = sysMenuService.treeByUserId(userId);
        return Result.ok(sysMenuVos);
    }

    @ApiOperation("退出登录")
    @DeleteMapping("/logout")
    public Result<Void> logout(){
        sysAuthService.logout();
        return Result.ok();
    }

}

















